#include "widget.h"
#include "ui_widget.h"

#include "mythread.h"
#include <QThread>
#include <QDebug>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    // 创建子线程
    MyThread *myThread = new MyThread;
    QThread *qThread = new QThread(this);
    myThread->moveToThread(qThread);

    // 处理MyThread::mySignal信号
    connect(myThread, &MyThread::mySignal, [this](){
        this->n++;
        ui->lcdNumber->display(this->n);
    });

    // 通过startThread信号，启动MyThread::supportThread 方法
    connect(this, &Widget::startThread, myThread, &MyThread::supportThread);

    // 为启动按钮设置信号和槽
    connect(ui->pushButton, &QPushButton::clicked, [=](){
        // 启动子线程
        qThread->start();
        myThread->setRun(true);

        // 发射信号，调用MyThread::supportThread();
        emit this->startThread();
    });

    // 停止功能
    connect(ui->pushButton_2, &QPushButton::clicked, [=](){
        myThread->setRun(false);
        qThread->quit();
    });

}

Widget::~Widget()
{
    delete ui;
}
